﻿package com.dykj.platform.iadapter.utils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import com.dykj.platform.framework.utils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import com.dykj.apps.web.sysmanager.dto.Tree;

/**
 * @author zhaolp
 * @time 2009-3-24
 */
public class TreeHelper{

	
	public static String getAllChildrenJSONTrees(List source){
		List root = TreeHelper.getRootTree(source);
		Iterator it = root.iterator();
		JSONArray result = new JSONArray();
		while(it.hasNext()){
			Tree tree = new Tree();
			Map item = (Map) it.next();
			TreeHelper.getAllChildrenTrees(source, item.get("functionid").toString(), tree);
			HashMap<String, Object> sub=TreeHelper.transTree(tree);	
			result.add(sub);
		}
		JSONArray json = JSONArray.fromObject(result);
		return json.toString();
	}
	public static List getRootTree(List source){
		List<Map<?, ?>> result = new ArrayList<Map<?, ?>>();
		Iterator it = source.iterator();
		while (it.hasNext()) {
			Map item = (Map) it.next();
			if (item.get("parentid") == null) {
				result.add(item);
			}
		}
		return result;
	}
	/**
	 * 获取所有孩子树
	 * 
	 * @param source
	 * @param treeid
	 * @param tree
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Tree getAllChildrenTrees(List source, String treeid, Tree tree) {
		List directChildrenTrees = TreeHelper.getDirectChildrenTrees(source, treeid);
		Map currentTree = TreeHelper.getTree(source, treeid);
		Tree tmp = null;
		if (StringUtils.isEmpty(tree.getFunctionid())) {
			TreeHelper.MapToObject(tree, currentTree);
			tmp = tree;
		} else {
			Tree tmptree = new Tree();
			TreeHelper.MapToObject(tmptree, currentTree);
			tmp = new Tree(tmptree.getFunctionid(), tmptree.getTitle(),
					tmptree.getHref(),tmptree.getChecked(), tree);
		}
		Iterator it = directChildrenTrees.iterator();
		while (it.hasNext()) {
			Map item = (Map) it.next();
			if (TreeHelper.isHaveParent(source, item)) {
				TreeHelper.getAllChildrenTrees(source, item.get("functionid").toString(),
								tmp);
			} else {
//				new Tree(currentTree.get("id").toString(), currentTree.get(
//						"text").toString(), currentTree.get("url").toString(),
//						tree);
				Tree tmptree = new Tree();
				TreeHelper.MapToObject(tmptree, currentTree);
				tmp = new Tree(tmptree.getFunctionid(), tmptree.getTitle(),
						tmptree.getHref(),tmptree.getChecked(), tree);
			}
		}
		return tree;
	}

	/**
	 * 把树转为json结构树
	 * 
	 * @param tree
	 * @return
	 */
	public static HashMap<String, Object> transTree(Tree tree) {
		HashMap<String, Object> value = new HashMap<String, Object>();
		value.put("functionid", tree.getFunctionid());
		value.put("title", tree.getTitle());
		if("1".equalsIgnoreCase(tree.getChecked())){
			value.put("checked", "1");
		}
		if (tree.hasChilds()) {
			List<HashMap<String, Object>> childs = new ArrayList<HashMap<String, Object>>();
			Iterator<Tree> itr = tree.getChildIterator();
			while (itr.hasNext()) {
				Tree tmp = itr.next();
				HashMap<String, Object> tmpv = TreeHelper
						.transTree(tmp);
				childs.add(tmpv);
			}
			value.put("children", childs);
		} else {
			value.put("leaf", true);
		}
		return value;
	}
	

	/**
	 * 判断是否有父结点
	 * 
	 * @param source
	 * @param transedTree
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static boolean isHaveParent(List source, Map transedTree) {
		boolean result = false;
		if (source.isEmpty() || source.size() < 1 || source == null
				|| transedTree.isEmpty() || transedTree == null) {
			return result;
		} else {
			if (transedTree.get("parentid") == null) {
				result = false;
			} else {
				Map parentTree = TreeHelper.getTree(source, transedTree.get(
						"parentid").toString());
				if (null == parentTree || parentTree.isEmpty()) {
					result = false;
				} else {
					if (source.contains(parentTree)) {
						result = true;
					} else {
						result = false;
					}
				}
			}
		}
		return result;
	}

	/**
	 * 获取所有的直接孩子结点
	 * 
	 * @param source
	 * @param treeid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List getDirectChildrenTrees(List source, String treeid) {
		List result = new ArrayList();
		Iterator it = source.iterator();
		while (it.hasNext()) {
			Map item = (Map) it.next();
			if (item.get("parentid") != null) {
				if (item.get("parentid").toString().equals(treeid)) {
					result.add(item);
				}
			}
		}
		return result;
	}

	/**
	 * 获取结点本身
	 * 
	 * @param source
	 * @param treeid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Map getTree(List source, String treeid) {
		Map result = null;
		if (source.isEmpty() || source.size() < 1
				|| StringUtils.isEmpty(treeid)) {
			return null;
		}
		Iterator it = source.iterator();
		while (it.hasNext()) {
			Map item = (Map) it.next();
			if (item.get("functionid").toString().equals(treeid)) {
				result = item;
				break;
			}
		}
		return result;
	}

	private static void MapToObject(Object object, Map<String, String> map) {
		try {
			BeanUtils.copyPropertiesLowerCase(object, map);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
